package qc.module.qms.algorithm.NumericCalc.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qc.module.qms.algorithm.NumericCalc.dto.QmsNumericCalcDest;
import qc.module.qms.algorithm.NumericCalc.dto.QmsNumericCalcInput;
import qc.module.qms.algorithm.NumericCalc.dto.QmsNumericCalcResult;
import qc.module.qms.algorithm.NumericCalc.dto.QmsNumericCalcUseMethod;
import qc.module.qms.algorithm.NumericCalc.method.*;

/**
 * QMS数值计算工具类，供外部调用，提供方法中自动根据计算方法进行调用对应的计算子类
 *
 * @author QuCheng Tech
 * @since 2025/9/10
 */
public class QmsNumericCalcUtil {
    private static final Logger logger = LoggerFactory.getLogger(QmsNumericCalcUtil.class);

    public static QmsNumericCalcResult calc(QmsNumericCalcDest dest, QmsNumericCalcUseMethod method, QmsNumericCalcInput input) {
        logger.debug("calc");
        //基本验证
        QmsNumericCalcMethodBase calcMethod = null;
        //根据计算方法中的内容指定调用的子类
        String methodContent = method.getContent();
        //动态反射指定调用计算方法类
        if (StringUtils.equalsIgnoreCase(methodContent, "prg:diff"))
            calcMethod = new QmsNumericCalcMethodDiff(dest, method, input);
        else if (StringUtils.equalsIgnoreCase(methodContent, "prg:linear"))
            calcMethod = new QmsNumericCalcMethodLinear(dest, method, input);
        else if (StringUtils.equalsIgnoreCase(methodContent, "prg:null"))
            calcMethod = new QmsNumericCalcMethodNull(dest, method, input);
        else if (StringUtils.equalsIgnoreCase(methodContent, "prg:point"))
            calcMethod = new QmsNumericCalcMethodPoint(dest, method, input);
        else if (StringUtils.equalsIgnoreCase(methodContent, "prg:simulate"))
            calcMethod = new QmsNumericCalcMethodSimulate(dest, method, input);

        if (calcMethod == null) {
            //未找到匹配的方法在计算结果中返回
            QmsNumericCalcResult result = new QmsNumericCalcResult();
            result.setSuccess(false);
            result.setMsg("根据计算方法[" + method.getMethodcode() + "]内容[" + methodContent + "]未找到匹配的计算方法，无法进行计算");

            return result;
        }


        //调用计算方法并获取结果
        //参考使用工厂模式或反射方式进行调用：https://developer.aliyun.com/article/1541583
        calcMethod.calc();

        return calcMethod.result;
    }
}
